package edu.cuit.svsp.controller;

import edu.cuit.svsp.entity.Video;
import edu.cuit.svsp.request.ResPage;
import edu.cuit.svsp.request.ResponseResult;
import edu.cuit.svsp.service.VideoService;
import edu.cuit.svsp.util.JWTUtil;
import edu.cuit.svsp.util.KeyUtil;
import edu.cuit.svsp.util.OssUtil;
import edu.cuit.svsp.vo.VideoAddVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @className: VideoController
 * @author: pjy
 * @description: 视频控制器
 * @date: 2024/7/3
 */
@Slf4j
@RestController
@RequestMapping("/video")
public class VideoController {
    @Resource
    private VideoService videoService;
    @Resource
    private OssUtil ossUtil;

    //分页查询
    @GetMapping("/pageByUid")
    public ResponseResult<ResPage> pageByUid(Integer page, Integer size, HttpServletRequest request) {
//        log.info("page:{},size:{}", page, size);
        int uid = JWTUtil.getuid(request.getHeader("Authorization"));
        ResPage<List<Video>> resPage = videoService.pageByUid(page, size, uid);
        return new ResponseResult<>(HttpStatus.OK.value(), "查询成功", resPage);
    }
    //上传视频
    @PostMapping("/upload")
    public ResponseResult upload(VideoAddVo vo, HttpServletRequest request) throws IOException {
//        log.info("vo:{}", vo);
        int uid = JWTUtil.getuid(request.getHeader("Authorization"));
        String videoUrl = ossUtil.upOss(vo.getVideo());
        String coverUrl = ossUtil.upOss(vo.getCover());

        Video video = new Video();
        video.setUid(uid);
        video.setTitle(vo.getTitle());
        video.setVideo(videoUrl);
        video.setCover(coverUrl);
        video.setCids(vo.getCids());
        //添加视频
        videoService.addVideo(video);
        //视频审核
//        ossUtil.check(videoUrl, video.getId());

        return new ResponseResult(HttpStatus.OK.value(), "上传成功",true);
    }

    //查询新视频
    @GetMapping("/findNew")
    public ResponseResult findNew() {
        List<Video> videos = videoService.findNew();
        return new ResponseResult(HttpStatus.OK.value(), "success",videos);
    }

    //通过id查询视频
    @GetMapping("/findById/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id) {
        Video video = videoService.findById(id);
        return new ResponseResult(HttpStatus.OK.value(), "success",video);
    }

    //添加播放量
    @GetMapping("addPlayNums/{id}/{key}")
    public ResponseResult addPlayNums(@PathVariable("id") Integer vid, @PathVariable("key") String key) {
        if (KeyUtil.removeKey(key)) {
            return new ResponseResult(HttpStatus.OK.value(), "success",videoService.addPlayNums(vid));
        }
        return new ResponseResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "fail",false);
    }

    //查询所有视频
    @GetMapping("/findAll/{page}/{size}")
    public ResponseResult findAll(@PathVariable int page, @PathVariable int size) {
        ResPage<List<Video>> resPage = videoService.findAll(page, size);
        return new ResponseResult(HttpStatus.OK.value(), "success",resPage);
    }

    //锁定视频
    @GetMapping("/lock/{id}")
    public ResponseResult lock(@PathVariable("id") Integer id) {
        videoService.lock(id);
        //通知up主视频锁定
        return new ResponseResult(HttpStatus.OK.value(), "success",true);
    }

    //手动审核视频
    @GetMapping("/pass/{id}")
    public ResponseResult pass(@PathVariable("id") Integer id) {
        videoService.pass(id);
        return new ResponseResult(HttpStatus.OK.value(), "success",true);
    }

    //根据视频状态查询视频
    @GetMapping("/findByState/{page}/{size}/{state}")
    public ResponseResult findByState(@PathVariable int page, @PathVariable int size, @PathVariable String state) {
        ResPage<List<Video>> resPage = videoService.findByState(page, size, state);
        return new ResponseResult(HttpStatus.OK.value(), "success",resPage);
    }

    //根据搜索框查询视频
    @GetMapping("/search/{page}/{size}/{state}/{input}")
    public ResponseResult search(@PathVariable int page, @PathVariable int size, @PathVariable String state, @PathVariable String input) {
        if(input.equals("0")){
            input="";
        }
        System.out.println("input="+input);
        ResPage<List<Video>> resPage = videoService.search(page, size, input, state);
        return new ResponseResult(HttpStatus.OK.value(), "success",resPage);
    }
}
